package com.zhucan.order.infrastructure.config;

import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.paths.AbstractPathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Lists.newArrayList;
import static springfox.documentation.builders.PathSelectors.regex;


/**
 * @author zhuCan
 * @description
 * @since 2022-07-07 09:56
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket adminServiceApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhucan"))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(newArrayList(securityContext())); // 开启swagger token验证支持
        docket.host("localhost:8081")
                .pathProvider(new AbstractPathProvider() {
                    @Override
                    protected String applicationPath() {
                        return "/";
                    }

                    @Override
                    protected String getDocumentationPath() {
                        return "/";
                    }
                });

        return docket;
    }


    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(securityPaths())
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(new SecurityReference("Authorization", authorizationScopes));
    }

    private Predicate<String> securityPaths() {
        return and(regex("/*"), not(regex("/*")));
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("dubbo + zookeeper 微服务架构")
                .description("kafka 事件驱动")
                .license("")
                .licenseUrl("apiInfo.licenseUrl")
                .version("v1.0")
                .build();
    }
}
